//
//  Device.swift
//  CosClub
//
//  Created by iOS on 2020/4/23.
//  Copyright © 2020 ios. All rights reserved.
//

import Foundation
import UIKit

public struct Device {
    public static let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
    public static let udid: String = UIDevice.current.identifierForVendor?.uuidString ?? ""
    public static let deviceScreenSize: String = "\(kScreenWidth) * \(kScreenHeight)"
    public static let systemName = UIDevice.current.systemName
    public static let deviceVersion: String = UIDevice.current.systemVersion
    public static let ratio: CGFloat = 3.0/4.0  // 高宽比
    
    public static var nowTime: Int {
        //获取当前时间
        let now = Date()
         
        // 创建一个日期格式器
        let dformatter = DateFormatter()
        dformatter.dateFormat = "yyyy年MM月dd日 HH:mm:ss"
        print("当前日期时间：\(dformatter.string(from: now))")
         
        //当前时间的时间戳
        let timeInterval:TimeInterval = now.timeIntervalSince1970
        let timeStamp = Int(timeInterval) * 1000
        return timeStamp
    }
    
}

public extension Bundle {
    static func bundle(nibName: String, anyClass: AnyClass? = nil, bundleName: String? = nil, moduleName: String? = nil) -> Any?{
        var trueAnyClass: AnyClass? = anyClass
        if trueAnyClass == nil {
            trueAnyClass = SwiftClassFromString(nibName, moduleName: moduleName)
        }
        
        guard trueAnyClass != nil else {
            return  Bundle.main.loadNibNamed(nibName, owner: nil, options: nil)?.first
        }
        
        guard let trueBundleName = bundleName else {
            return Bundle.main.loadNibNamed(nibName, owner: nil, options: nil)?.first
        }
        
        let cbundle = Bundle(for:trueAnyClass!)
        let path = cbundle.bundlePath + "/\(trueBundleName).bundle"
        print(path)
        let mbundle = Bundle.init(path: path)
        guard let bundle = mbundle else {
            return Bundle.main.loadNibNamed(nibName, owner: nil, options: nil)?.first
        }
        return bundle.loadNibNamed(nibName, owner: nil, options: nil)?.first
    }
}

public extension UINib {
    static func nib(nibName name: String ,anyClass: AnyClass? = nil, bundle bundleOrNil: Bundle?, bundleName: String? = nil, moduleName: String? = nil) -> UINib?{
        
        if bundleOrNil != nil {
            return self.init(nibName: name, bundle: bundleOrNil)
        }
        
        var trueAnyClass: AnyClass? = anyClass
        if trueAnyClass == nil {
            trueAnyClass = SwiftClassFromString(name, moduleName: moduleName)
        }
        
        guard trueAnyClass != nil else {
            return self.init(nibName: name, bundle: bundleOrNil)
        }
        
        guard let trueBundleName = bundleName else {
            return self.init(nibName: name, bundle: bundleOrNil)
        }
        
        let cbundle = Bundle(for:trueAnyClass!)
        let path = cbundle.bundlePath + "/\(trueBundleName).bundle"
        print(path)
        let mbundle = Bundle.init(path: path)
        guard let bundle = mbundle else {
            return self.init(nibName: name, bundle: bundleOrNil)
        }
        return self.init(nibName: name, bundle: bundle)
        
    }
}
